<?php
/**
 * @author ykm 2016-03-11
 * @describe 店铺货品冻结库存
*/
class erpapi_shop_response_plugins_order_refundapply extends erpapi_shop_response_plugins_order_abstract
{

    public function convert(erpapi_shop_response_abstract $platform)
    {
        $refundApplySdf = array();

        if ($platform->_tgOrder['order_id']) {
            $refundApplyModel = app::get('ome')->model('refund_apply');

            $refund_money = 0;
            if ($platform->_ordersdf['payed'] > $platform->_ordersdf['total_amount']) {
                $refund_money = bcsub($platform->_ordersdf['payed'], $platform->_ordersdf['total_amount'],3);
            }

            if($refund_money <= 0) return array();

            // 判断是否有申请中的
            $has = $refundApplyModel->getList('apply_id',array('order_id'=>$platform->_tgOrder['order_id'],'status'=>array('0','1','2')),0,1);
            if ($has) {
                return array();
            }

            $create_time = $platform->_ordersdf['lastmodify'] ? kernel::single('ome_func')->date2time($platform->_ordersdf['lastmodify']) : time();
            $refundApplySdf = array(
                'order_id'        => $platform->_tgOrder['order_id'],
                'refund_apply_bn' => $refundApplyModel->gen_id(),
                'pay_type'        => 'online',
                'money'           => $refund_money,
                'refunded'        => '0',
                'memo'            => '订单编辑产生的退款申请',
                'create_time'     => $create_time,
                'status'          => '2',
                'shop_id'         => $platform->__channelObj->channel['shop_id'],
            );
        }


        return $refundApplySdf;
    }

    /**
     * 订单完成后处理
     **/
    public function postCreate($order_id,$refundapply)
    {
    }

    /**
     * 更新后操作
     *
     * @return void
     * @author 
     **/
    public function postUpdate($order_id,$refundapply)
    {
        $refundApplyModel = app::get('ome')->model('refund_apply');

        $refundapply['order_id']        = $order_id;

        $refundApplyModel->create_refund_apply($refundapply);

        $logModel = app::get('ome')->model('operation_log');
        $logModel->write_log('order_edit@ome',$order_id,'退款申请');
    }
}